import platformApi from '../../platforms/index'
export default function getAlipayApi ({ platform } = {}) {
  const clientPlatform = platform || this.context.PLATFORM
  if (!clientPlatform) {
    throw new Error('未能获取客户端平台信息，请主动传入platform')
  }
  const config = this._getConfig(clientPlatform)
  if (!config.oauth || !config.oauth.alipay) {
    throw new Error(`请在公用模块uni-id的config.json中添加${clientPlatform}平台支付宝登录配置项`)
  }
  const argsRequired = ['appid', 'privateKey']
  argsRequired.forEach((item) => {
    if (!config.oauth.alipay[item]) {
      throw new Error(`请在公用模块uni-id的config.json中添加配置项：${clientPlatform}.oauth.alipay.${item}`)
    }
  })
  const alipayApi = platformApi.initAlipay({ ...config.oauth.alipay, clientType: clientPlatform })
  return alipayApi
}
